package com.fcy.teamwork.business.projectlife

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.fcy.teamwork.R
import com.fcy.teamwork.base.BaseRVAdapter
import com.fcy.teamwork.bean.PlanNode
import com.fcy.teamwork.bean.User
import com.fcy.teamwork.business.SelectFromGroupDiaFragment
import com.fcy.teamwork.databinding.FooterViewProjectsplanBinding
import com.fcy.teamwork.databinding.ItemArrawDecorationBinding
import com.fcy.teamwork.databinding.ItemMcvNodeBinding
import com.fcy.teamwork.databinding.ItemProjectPlanBinding
import com.fcy.teamwork.pojo.ProjectNode
import com.fcy.teamwork.widget.PortraitView
import com.google.android.material.card.MaterialCardView
import com.google.android.material.floatingactionbutton.FloatingActionButton
import java.text.SimpleDateFormat

class ProjectsPlanAdapter(private val vm: ProjectControllerVM) :
    BaseRVAdapter<List<ProjectNode>, ViewBinding>() {
    inner class Indicator(
        private val tvShow: TextView,
        private val fab: FloatingActionButton,
        private val fabCancel: FloatingActionButton,
        private val fabNewNode: FloatingActionButton
    ) {

        private var curMCV: MaterialCardView? = null
        private var planNode: ProjectNode? = null
        private val curFAB: FloatingActionButton?
            get() {
                val curNode = planNode
                return if (curNode == null) null
                else if (curNode.state == 1) {
                    fabCancel
                } else fab
            }

        init {
            fabNewNode.setOnClickListener {
                AddNewPlanNodeFrag().show(
                    (it.context as? AppCompatActivity)?.supportFragmentManager
                        ?: throw Exception("supportFragmentManager 获取失败"), ""
                )
            }
            fab.setOnClickListener {
                planNode?.state = 1
                planNode?.enTmp = System.currentTimeMillis()
                curMCV?.findViewById<PortraitView>(R.id.pv_icon)?.setImageResource(R.color.yellow)
                fabCancel.show()
                fab.hide()
                vm.updateNodeInfo(planNode!!)
            }
            fabCancel.setOnClickListener {
                fab.show()
                fabCancel.hide()
                planNode?.state = 0
                planNode?.enTmp = 0L
                curMCV?.findViewById<PortraitView>(R.id.pv_icon)
                    ?.setImageResource(R.color.base_line_color)
                vm.updateNodeInfo(planNode!!)
            }
            fab.hide()
            fabCancel.hide()
            fabNewNode.hide()
        }

        fun refreshInfo(planNode: ProjectNode, cv: MaterialCardView) {
            curFAB?.hide()
            if (this.planNode === planNode) {
                cancelSelect()
                return
            }
            cancelSelect()
            select(planNode, cv)
        }

        private fun cancelSelect() {
            curMCV?.strokeColor = 0
            curMCV?.strokeWidth = 0
            curMCV = null
            planNode = null
            curFAB?.hide()
            fabNewNode.hide()
        }

        private fun select(planNode: ProjectNode, cv: MaterialCardView) {
            vm.buildingBode.step = planNode.step
            tvShow.text = processNodeInfo(planNode)
            this.planNode = planNode
            this.curMCV = cv
            cv.strokeColor = 0xFFBB86FC.toInt()
            cv.strokeWidth = 2
            curFAB?.show()
            fabNewNode.show()
        }

        private fun processNodeInfo(node: ProjectNode): String {
            return node.toString()
        }
    }

    private var indicator: Indicator? = null
    fun initIndicator(
        tvShow: TextView,
        fab: FloatingActionButton,
        fabCancel: FloatingActionButton,
        fabNewNode: FloatingActionButton
    ) {
        indicator = Indicator(tvShow, fab, fabCancel, fabNewNode)
    }

    override fun createBinding(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding = ItemProjectPlanBinding.inflate(inflater, parent, false)


    override fun bindView(holder: VH, position: Int, data: List<ProjectNode>) {
        (holder.binding as? ItemProjectPlanBinding)?.apply {
            root.removeAllViews()
            val inflater = LayoutInflater.from(root.context)
            for (y in data.indices) {
                val mcv = ItemMcvNodeBinding.inflate(inflater, root, false)
                mcv.root.setOnClickListener {
                    indicator?.refreshInfo(data[y], mcv.root)
                }
                val colorId = if (data[y].state == 1)
                    R.color.yellow
                else R.color.base_line_color
                mcv.pvIcon.setImageResource(
                    colorId
                )
                mcv.tvTitle.text = data[y].title
                root.addView(mcv.root)
            }
        }
    }

    override fun hasFooter(): Boolean = true
    override fun hasDecoration(): Boolean = true
    override fun bindDecorationView(holder: VH, position: Int) {}
    override fun createDecorationBinding(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return ItemArrawDecorationBinding.inflate(inflater, parent, false)

    }

    override fun createFootBinding(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return FooterViewProjectsplanBinding.inflate(inflater, parent, false)
    }

    override fun bindFooterView(holder: VH, position: Int) {
        (holder.binding as? FooterViewProjectsplanBinding)?.apply {
            ibAddNewPlanNode.setOnClickListener {
                vm.buildingBode.step = position / 2 + 2
                AddNewPlanNodeFrag().show(
                    (root.context as? AppCompatActivity)?.supportFragmentManager
                        ?: throw Exception("supportFragmentManager 获取失败"), ""
                )
            }
        }
    }
}